<?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Upload extends CI_Controller
{
    
    function __construct() {
        parent::__construct();
        $this->load->library('breadcrumb');
    }
    
    function index() {
        
        //rule type pada function ini: Read
        $this->rule->type('R');
        
        //title
        $this->layout->set_title('Upload');
        
        //breadcrumb/untuk navigasi
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb('Beranda', site_url('admin'));
        $this->breadcrumb->add_crumb('Contoh', site_url('upload#'));
        $this->breadcrumb->add_crumb('Upload');

        //jquery form
        $this->layout->set_include('js', 'themes/back/bundle/jquery.form/jquery.form.min.js', 'header');

        //judul besar
        $data['primary_title'] = '<i class="fa fa-bars"></i> Contoh';
        $data['sub_primary_title'] = 'Contoh-contoh coding aja';
        
        //untuk crop dan atau resize image
        $this->load->helper('image');

        //menggunakan theme back
        $this->layout->theme('back', 'upload/index', $data);
    }

    function do_upload(){
        $config['upload_path'] = FCPATH . '/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            $data = array('error1' => $this->upload->display_errors());
            $this->layout->theme('back', 'upload/index', $data);
        }else{
            $data = array('upload_data' => $this->upload->data());
            $this->layout->theme('back', 'upload/upload_success', $data);
        }
    }

    function do_ajax_upload(){
        $config['upload_path'] = FCPATH . '/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width']  = '2048';
        $config['max_height']  = '1536';

        $this->load->library('upload', $config);

        //untuk crop dan atau resize image
        $this->load->helper('image');

        if ( ! $this->upload->do_upload('ajaxfile')){
            $data = array('error2' => $this->upload->display_errors());
            $this->load->view('back/layouts/upload/dynamic', $data);
        }else{
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('back/layouts/upload/dynamic', $data);
        }
    }

}
